package com.yzyz.data;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import com.yzyz.bean.region.RegionBean;
import com.yzyz.bean.scenic.ScenicBean;

/**
 * 这个类是一个缓冲，它的作用是用来加载所有的 景区分组
 * 景区分组中又包含了景区以及景区描述和景区中的景点
 * @author David Sun
 * @Date   2010-3-26
 *
 */
public class RegionStore {
	private HashMap<Integer,RegionBean> regions = new HashMap<Integer, RegionBean>(10);
	
	/**
	 * 初始化
	 * @param regions
	 */
	public synchronized void init(List<RegionBean> regions){
		for(RegionBean region: regions){
			this.regions.put(region.getId(), region);
		}
	}
	
	/**
	 * 通过景区的id来查找景区
	 * @param scenicId
	 */
	public synchronized ScenicBean getScenicById(int scenicId){
		Set<Entry<Integer, RegionBean>> groups = regions.entrySet();
		for(Entry<Integer, RegionBean> entry: groups){
			RegionBean region = (RegionBean)entry.getValue();
			if(region.getScenics() != null){
				for(ScenicBean scenic: region.getScenics()){
					if(scenic.getId() == scenicId){
						return scenic;
					}
				}
			}
		}
		return null;
	}
	
	/**
	 * 通过景区名字查找景区
	 * @param name
	 * @return
	 */
	public synchronized ScenicBean searchScenicByName(String name){
		Set<Entry<Integer, RegionBean>> groups = regions.entrySet();
		for(Entry<Integer, RegionBean> entry: groups){
			RegionBean region = (RegionBean)entry.getValue();
			if(region.getScenics() != null){
				for(ScenicBean scenic: region.getScenics()){
					if(scenic.getName().trim().equals(name.trim())){
						return scenic;
					}
				}
			}
		}
		return null;
	}
	
	/**
	 * 通过分组名字查找分组
	 * @param name
	 * @return
	 */
	public synchronized RegionBean searchByName(String name){
		Set<Entry<Integer, RegionBean>> groups = regions.entrySet();
		for(Entry<Integer, RegionBean> entry: groups){
			RegionBean region = (RegionBean)entry.getValue();
			if(region.getName().trim().equals(name.trim())){
				return region;
			}
		}
		return null;
	}
	
	/**
	 * 获得某个地区下的所有景区
	 * @param regionId
	 * @return
	 */
	public synchronized  List<ScenicBean> getScenics(int regionId) {
		RegionBean region = this.regions.get(regionId);
		return region.getScenics();
	}
	
	/**
	 * 根据景区分组的ID来获取某个分组
	 * @param regionId
	 * @return
	 */
	public synchronized RegionBean getRegion(int regionId){
		return this.regions.get(regionId);
	}
}
